package com.example.echoewish.ui.main_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.Post;
import com.example.echoewish.repository.PostMessageRepository;
import com.example.echoewish.util.LogUtil;

import java.io.File;
import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishViewModel extends ViewModel {
    MutableLiveData<Post> postMutableLiveData;
    MutableLiveData<String> responseMutableLiveData;

    public PublishViewModel()
    {
        postMutableLiveData=new MutableLiveData<>();
        responseMutableLiveData=new MutableLiveData<>();
    }

    public void publishPost(File image)
    {
        PostMessageRepository.getInstance().publishPost(image,postMutableLiveData.getValue(),new PublishPostCallBack());
    }

    private class PublishPostCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                responseMutableLiveData.postValue(response.body().string());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            new LogUtil().w(LogUtil.CALLBACK_TAG,"发布帖子 回调失败"+t);
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
        }
    }
}
